﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SundayWishBLL;
using SundayWishModel.Entities;
using System.IO;
public partial class Admin_publicdish_addpublicdish : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ContentPlaceHolder UserMaster = (ContentPlaceHolder)Page.Master.Master.FindControl("ContentPlaceHolder1");
        ((Label)UserMaster.FindControl("lbpagename")).Text = "添加经典食谱";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        PublicDishHelper pdh = new PublicDishHelper();
        Dish dish = new Dish();
        dish.DishName = tbDishName.Text;
        dish.DishMaterial = tbMaterial.Text;
        dish.DishStep = tbStep.Text;
        dish.DishIntroduction = tbIntroduction.Text;
        dish.LastModifyDate = DateTime.Now;
        dish.CreateDate = DateTime.Now;
        dish.DishKind = ddlKind.SelectedValue;
        dish.DishPublic = 1;
        dish.DishImage = "defaultimg.gif";
        dish.DishStep = dish.DishStep.Replace("\r\n", "<br/>");
        dish.DishIntroduction = dish.DishIntroduction.Replace("\r\n", "<br/>");
        dish.DishImage = lbimg.Text;
        pdh.AddDish(dish);
        Page.RegisterStartupScript("ServiceManHistoryButtonClick2", "<script>alert(\"添加成功\");this.location.href='privatedishlist.aspx';</script>");
    }

    protected void btnupload_Click(object sender, EventArgs e)
    {
        if (attachment_upload.HasFile)
        {
            // Get File Extension
            Int32 tmpIndex = attachment_upload.FileName.LastIndexOf('.');
            String fileExtension = attachment_upload.FileName.Substring(tmpIndex + 1,
                attachment_upload.FileName.Length - tmpIndex - 1);
            fileExtension = fileExtension.ToLower();
            String fileName = attachment_upload.FileName.Substring(0, tmpIndex);
            long tick = DateTime.Now.Ticks;
            Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
            // Generate File Path
            try
            {
                int a = ran.Next();
                string destFilePath = Server.MapPath("~/image/" + a.ToString() + attachment_upload.FileName);
                string smalldestFilePath = Server.MapPath("~/image/s" + a.ToString() + attachment_upload.FileName);
                string temp = Server.MapPath("~/image/" + "temp" + a.ToString() + attachment_upload.FileName);
                attachment_upload.SaveAs(temp);
                pic.ImageUrl = "~/images/" +a.ToString() + attachment_upload.FileName;
                lbimg.Text = a.ToString() + attachment_upload.FileName;
                WebHelper.MakeThumbnail(temp, destFilePath, 150, 150, "HW", null);
                WebHelper.MakeThumbnail(temp, smalldestFilePath, 120, 120, "HW", null);
                System.IO.File.Delete(temp);
            }
            catch
            {
            }
        }
        else
        {
        }
    }
    protected void btnupload_Cancel(object sender, EventArgs e)
    {
        if (pic.ImageUrl != "~/image/defaultimg.gif")
        {
            File.Delete(Server.MapPath(pic.ImageUrl));
            pic.ImageUrl = "~/image/defaultimg.gif";
        }
    }
}
